﻿using System;
using System.Runtime.InteropServices;
using OdsServer.Common.DataTransfer;

namespace OdsServer.Common.Packet
{
    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
    public unsafe struct MessagePacket: IBasePacket
    {
        internal PacketHeader header;
        public const int MESSAGE_LENGTH = Byte.MaxValue;
        internal MarshalMessageDataChunk msg;

        public MessagePacket(string message)
        {
            this.header.type    = PacketType.MessagePacket;
            char[] msgVal       = new char[MESSAGE_LENGTH];

            message.CopyTo(0, msgVal, 0, message.Length);

            this.msg = new MarshalMessageDataChunk(msgVal);

            // unable to calculate sizeof MessagePacket due to marshaling serialization
            this.header.lenght = sizeof(PacketHeader) + this.msg.GetDataChunkBytes();
        }

        public string GetMessage()
        {
            return new string(this.msg.GetDataChunk());
        }

        public PacketHeader GetHeader()
        {
            return this.header;
        }
    }
}